<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;


class LoginController extends BaseController
{
    public function index()
    {

        $code = $this->request->param('code');

        //获取openid
        $openid = $code;

        $user = Db::name('agent')->where('openid',$openid)->find();
        if ($user){
            // 生成 token
            $token = \app\utils\JwtService::createToken(['uid' => $user['id']]);
            return json(['code' => 200, 'token' => $token]);
        }else{
            $user = Db::name('agent')->save([
                'openid' => $openid,
                'mobile' => 1352626437,
                'nick_name' => '测试'.rand(1000,9999),
                'avatar' => ''
            ]);

            $token = \app\utils\JwtService::createToken(['uid' => $user['id']]);
            return json(['code' => 200, 'token' => $token]);
        }

    }


}
